<?php
class Train extends MY_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->model('model_train');
		$this->load->model('model_ticket');
		$this->load->model('model_rate');
    }
    public function findday($orgDate){
	    $strto = strtotime($orgDate);
	    $retDAY = date('Y-m-d', mktime(0,0,0,date('m',$strto),date('d',$strto),date('Y',$strto)));
	    return $retDAY;
	}
	public function trainall(){
		$data['abouts'] = $this->abouts();
		$data['destinations'] = $this->destinations(6);
		$data['listtrains'] = $this->listtrains(20);
		$data['listregions'] = $this->listregions();
		$data['listquestion'] = $this->faqs();
		$tickets = array();
		foreach($data['listtrains'] as $k => $items){
			$tickets = $this->model_rate->getcheaptickets($items['train_id']);
			if($tickets != NULL){
				$data['listtrains'][$k]['softseat'] = $tickets['softseat'];
				$data['listtrains'][$k]['hardberth'] = $tickets['hardberth'];
				$data['listtrains'][$k]['softberth'] = $tickets['softberth'];
				$data['listtrains'][$k]['vipberth'] = $tickets['vipberth'];
			}else{
				$data['listtrains'][$k]['softseat'] = 0;
				$data['listtrains'][$k]['hardberth'] = 0;
				$data['listtrains'][$k]['softberth'] = 0;
				$data['listtrains'][$k]['vipberth'] = 0;
			}
		}
		//$this->debug($data['listtrains']);
		$data['title'] = "Tourist trains to sapa";
		$this->load->view('train/trainall/layout',$data);
	}
	public function trainfrom(){
		$rewrite = $this->uri->segment(2);
		$region = $this->model_rate->getregion($rewrite);
		if($region == NULL){ redirect(base_url());}
		$id = $region['region_id'];
		$data['listdesti'] = $this->model_rate->getdestina($id);
		$tickets = array();
		foreach($data['listdesti'] as $k => $items){
			$tickets = $this->model_ticket->getcheaptickets($items['desti_id']);
			if($tickets != NULL){
				$data['listdesti'][$k]['softseat'] = $tickets['softseat'];
				$data['listdesti'][$k]['hardberth'] = $tickets['hardberth'];
				$data['listdesti'][$k]['softberth'] = $tickets['softberth'];
				$data['listdesti'][$k]['vipberth'] = $tickets['vipberth'];
			}else{
				$data['listdesti'][$k]['softseat'] = 0;
				$data['listdesti'][$k]['hardberth'] = 0;
				$data['listdesti'][$k]['softberth'] = 0;
				$data['listdesti'][$k]['vipberth'] = 0;
			}
		}
		//$this->debug($data['listdesti']);
		//$data['tickets'] = $tickets;
		$data['region'] =  $region;
		$data['abouts'] = $this->abouts();
		$data['destinations'] = $this->destinations(6);
		$data['listtrains'] = $this->listtrains(20);
		$data['listregions'] = $this->listregions();
		$data['listquestion'] = $this->faqs();
		$data['title'] = "Train from ".$region['region_title'];
		$this->load->view('train/trainfrom/layout',$data);
	}
	public function traindetail(){
	    $url = $this->uri->segment(2);
		$data['result'] = $this->model_ticket->getdest($url);
		if($data['result'] == NULL){ redirect(base_url());}
		$id = $data['result']['desti_id'];
		$data['title'] = "Train from ".strtolower($data['result']['fromname'])." to ".strtolower($data['result']['toname']);
		$now = strtotime(date("m/d/Y"));
		$tomorow = date('m/d/Y',mktime(0,0,0,date('m',$now),date('d',$now) + 1,date('Y',$now)));
	    $curstart = $this->findday($tomorow);
		//echo $id; return false;
		$data['listticket'] = $this->model_ticket->gettickets($id,$curstart);
		$data['listtrains'] = $this->listtrains(20);
		$data['destinations'] = $this->destinations(6);
		$data['listregions'] = $this->listregions();
		$data['listquestion'] = $this->faqs();
		$data['abouts'] = $this->abouts();
		//$this->debug($data['listticket']);
		$this->load->view('train/traindetail/layout',$data);
	}
    public function detail() {
		$id = $this->uri->segment(2);
		$data['abouts'] = $this->abouts();
		$data['listtrains'] = $this->listtrains(20);
		$data['destinations'] = $this->destinations(6);
		$data['listregions'] = $this->listregions();
		$data['curtrain'] = $this->model_train->getdata($id);
		if($data['curtrain'] == NULL){ redirect(base_url());}
		$now = strtotime(date("m/d/Y"));
	    $tomorow = date('m/d/Y',mktime(0,0,0,date('m',$now),date('d',$now) + 1,date('Y',$now)));
	    $nexttomor = date('m/d/Y',mktime(0,0,0,date('m',$now),date('d',$now) + 2,date('Y',$now)));
	    $curtrain = $data['curtrain'];
	    $curtype = 1;
	    $adults = 1;
	    $data['typetick'] = 2;
	    $curstart = $this->findday($tomorow);
	    $curend = $this->findday($nexttomor);
		//echo $data['curtrain']['train_id']; die();
	    $data['liststart'] = $this->model_rate->getstart($data['curtrain']['train_id'],$curtype,$curstart);
		//$this->debug($data['liststart']);
	    $data['listend'] = $this->model_rate->getend($data['curtrain']['train_id'],2,$curend);
		$data['trainid'] = $data['curtrain']['train_id'];
		$data['curstart'] = $tomorow;
		$data['curendate'] = $nexttomor;
		$data['adults'] = $adults;
		$data['listquestion'] = $this->faqs();
		$data['related'] = $this->model_train->related($data['curtrain']['train_id']);
		//$this->debug($data['related']);
        $data['title'] = $data['curtrain']['train_name']." - Hanoi Sapa Train";
        $this->load->view('train/layout',$data);
    }
}
?>